package com.fintech.third.utils;

import java.util.Date;
import java.util.Map;

import com.fintech.third.dao.MoxieTaskInfoMapper;
import com.fintech.third.model.MoxieTaskInfo;
import com.marvels.common.utils.MarvelsLogUtil;

/**
 * 
 * @ClassName:  MoXieCarrierForMall   
 * @Description:  魔蝎运营商数据任务操作接口
 * @author: 富爵金服-李瑶
 * @date:   2017年10月30日 下午4:36:35     
 * @Copyright: 2017 富爵金服
 * 注意：本内容仅限于富爵金服内部传阅
 */
public class MoXieCarrierForMall {

	private MoxieTaskInfoMapper moxieTaskInfoMapper=(MoxieTaskInfoMapper)ApplicationContextGet.getApplicationContext().getBean("moxieTaskInfoMapper");
	
	/**
	 * 
	 * @Title: insertTask   
	 * @Description:  插入魔蝎任务(1000任务插入成功，1002任务已有且在进行中，1003任务已执行成功)
	 * @param:      
	 * @return: void      
	 * @throws
	 */
	public void startMoxieTask(Map<String,String> map) {
		String taskId=map.get("task_id");
		String loanId=map.get("loanId");
		String mobile=map.get("mobile");
		
		MoxieTaskInfo moxieTaskInfo = moxieTaskInfoMapper.getMoxieTaskCur(loanId,"1");//查看当前任务表
		if(moxieTaskInfo==null){//没有数据
			MoxieTaskInfo moxieTaskInfoHis = moxieTaskInfoMapper.getMoxieTaskHis(loanId,"1");//查看历史表
			if(moxieTaskInfoHis==null){
				insertTask(map);
				return;
			}
			String statusHis = moxieTaskInfoHis.getResult();
			if(!"2".equals(statusHis)){
				insertTask(map);
				return;
			}
			return;
		}
		String result = moxieTaskInfo.getResult();
		if("0".equals(result)||"1".equals(result)||"-1".equals(result)){//处于处理中，或者尚未处理，或者尚未获取到数据的状态，直接挂起
			return;
		}
		if("2".equals(result)){//成功时不做任何操作
			return;
		}
		moxieTaskInfoMapper.updateMoxieTaskInfo(moxieTaskInfo.getId(), "0");
		
		
		
	}
	
	/**
	 * 
	 * @Title: getMoXieTaskResult   
	 * @Description: 查询魔蝎运营商数据状态（0）
	 * @param:      
	 * @return: int      
	 * @throws
	 */
	public String getMoXieTaskResult(String loanId){
		MoxieTaskInfo moxieTaskInfo = moxieTaskInfoMapper.getMoxieTaskCur(loanId,"1");//查看当前任务表
		if(moxieTaskInfo==null){//没有数据
			MoxieTaskInfo moxieTaskInfoHis = moxieTaskInfoMapper.getMoxieTaskHis(loanId,"1");//查看历史表
			if(moxieTaskInfoHis==null){
				return "1001";//尚未生成查询任务
			}
			String statusHis = moxieTaskInfoHis.getResult();
			if("0".equals(statusHis)||"1".equals(statusHis)){
				return "1001";//尚未生成任何任务
			}
			return statusHis;
		}
		String result = moxieTaskInfo.getResult();
		return result;
		
	}
	

	/**
	 * 
	 * @Title: insertTask   
	 * @Description:  
	 * @param:  map 任务参数 type 0新增
	 * @return: void      
	 * @throws
	 */
	private void insertTask(Map<String,String> map){
		String taskId=map.get("task_id");
		String loanId=map.get("loanId");
		String mobile=map.get("mobile");
		MarvelsLogUtil.getInstance().info("==================添加运营商任务信息开始,loanId" + loanId + ",任务taskId" + taskId+",mobile"+mobile);
		MoxieTaskInfo moxieTaskInfo=new MoxieTaskInfo();
		moxieTaskInfo.setCreatedBy("sys");
		moxieTaskInfo.setUpdatedBy("sys");
		moxieTaskInfo.setCreatedDate(new Date());
		moxieTaskInfo.setLoanId(loanId);
		moxieTaskInfo.setTaskId(taskId);
		moxieTaskInfo.setUpdatedDate(new Date());
		moxieTaskInfo.setMobile(mobile);
		moxieTaskInfo.setType("1");
		moxieTaskInfo.setResult("0");
		moxieTaskInfoMapper.insertMoxieTaskInfo(moxieTaskInfo);
		MarvelsLogUtil.getInstance().info("==================添加运营商任务信息结束,loanId" + loanId + ",任务taskId" + taskId+",mobile"+mobile+",开始修改添加运营商任务信息状态");

	}
}
